import type {
  Host,
  Department,
  Project,
  Module,
  HostsBlock,
  Log,
  HostTypeDefinition,
} from "../types";

// Update the mock data to include more host types
// Update the mockHostTypes array

const mockHostTypes: HostTypeDefinition[] = [
  {
    id: "1",
    code: "frontend",
    name: "前端",
    description: "前端服务和应用",
    color: "#3b82f6",
    icon: "GlobalOutlined",
    createdAt: "2023-05-10T08:00:00Z",
  },
  {
    id: "2",
    code: "backend",
    name: "后端",
    description: "后端服务和API",
    color: "#8b5cf6",
    icon: "ApiOutlined",
    createdAt: "2023-05-10T08:05:00Z",
  },
  {
    id: "3",
    code: "database",
    name: "数据库",
    description: "数据库服务",
    color: "#10b981",
    icon: "DatabaseOutlined",
    createdAt: "2023-05-10T08:10:00Z",
  },
  {
    id: "4",
    code: "cache",
    name: "缓存",
    description: "缓存服务",
    color: "#f59e0b",
    icon: "CloudOutlined",
    createdAt: "2023-05-10T08:15:00Z",
  },
  {
    id: "5",
    code: "dev",
    name: "开发",
    description: "开发环境",
    color: "#3b82f6",
    icon: "CodeOutlined",
    createdAt: "2023-05-10T08:20:00Z",
  },
  {
    id: "6",
    code: "test",
    name: "测试",
    description: "测试环境",
    color: "#f59e0b",
    icon: "BugOutlined",
    createdAt: "2023-05-10T08:25:00Z",
  },
  {
    id: "7",
    code: "prod",
    name: "生产",
    description: "生产环境",
    color: "#ef4444",
    icon: "RocketOutlined",
    createdAt: "2023-05-10T08:30:00Z",
  },
];

const mockDepartments: Department[] = [
  {
    id: "2",
    name: "孩子王部门",
    code: "HZW",
    description: "负责孩子王相关业务和项目",
    color: "#8b5cf6",
  },
];

const mockProjects: Project[] = [
  {
    id: "2",
    name: "订单系统",
    departmentId: "2",
    environment: "测试",
    description: "订单处理和管理系统",
    createdAt: "2023-05-16T10:15:00Z",
  },
];

// Mock data for modules
const mockModules: Module[] = [
  {
    id: "1",
    name: "用户认证模块",
    projectId: "1",
    description: "处理用户登录、注册和认证",
    createdAt: "2023-05-21T13:45:00Z",
  },
  {
    id: "2",
    name: "用户资料模块",
    projectId: "1",
    description: "管理用户个人资料和设置",
    createdAt: "2023-05-22T09:20:00Z",
  },
  {
    id: "3",
    name: "订单创建模块",
    projectId: "2",
    description: "处理订单创建和提交",
    createdAt: "2023-05-23T10:30:00Z",
  },
  {
    id: "4",
    name: "订单支付模块",
    projectId: "2",
    description: "处理订单支付流程",
    createdAt: "2023-05-24T15:15:00Z",
  },
  {
    id: "5",
    name: "支付接口模块",
    projectId: "3",
    description: "集成各种支付渠道接口",
    createdAt: "2023-05-25T11:40:00Z",
  },
  {
    id: "6",
    name: "商品列表模块",
    projectId: "4",
    description: "展示和管理商品列表",
    createdAt: "2023-05-26T14:50:00Z",
  },
];

// Mock data for hosts blocks
const mockHostsBlocks: HostsBlock[] = [
  {
    id: "1",
    name: "用户中心开发环境",
    projectId: "1",
    moduleId: "1",
    content: `# 用户中心开发环境
172.172.178.9 user.dev.example.com
172.172.178.9 auth.dev.example.com
# 172.172.178.9 api.user.dev.example.com
172.172.178.93 admin.user.dev.example.com`,
    description: "用户中心开发环境 Hosts 配置",
    environment: "开发",
    createdAt: "2023-05-27T08:30:00Z",
    updatedAt: "2023-05-27T08:30:00Z",
    createdBy: "管理员",
  },
  {
    id: "2",
    name: "用户资料服务",
    projectId: "1",
    moduleId: "2",
    content: `# 用户资料服务
172.172.178.10 profile.dev.example.com
172.172.178.10 api.profile.dev.example.com`,
    description: "用户资料服务 Hosts 配置",
    environment: "开发",
    createdAt: "2023-05-28T10:15:00Z",
    updatedAt: "2023-05-28T10:15:00Z",
    createdBy: "管理员",
  },
  {
    id: "3",
    name: "订单系统测试环境",
    projectId: "2",
    moduleId: "3",
    content: `# 订单系统测试环境
172.172.178.11 order.test.example.com
172.172.178.11 api.order.test.example.com
# 172.172.178.11 admin.order.test.example.com`,
    description: "订单系统测试环境 Hosts 配置",
    environment: "测试",
    createdAt: "2023-05-29T09:45:00Z",
    updatedAt: "2023-05-29T09:45:00Z",
    createdBy: "管理员",
  },
  {
    id: "4",
    name: "订单支付服务",
    projectId: "2",
    moduleId: "4",
    content: `# 订单支付服务
172.172.178.12 payment.order.test.example.com
172.172.178.12 api.payment.order.test.example.com`,
    description: "订单支付服务 Hosts 配置",
    environment: "测试",
    createdAt: "2023-05-30T14:20:00Z",
    updatedAt: "2023-05-30T14:20:00Z",
    createdBy: "管理员",
  },
  {
    id: "5",
    name: "支付网关生产环境",
    projectId: "3",
    moduleId: "5",
    content: `# 支付网关生产环境
172.172.178.13 payment.prod.example.com
172.172.178.13 api.payment.prod.example.com
# 172.172.178.13 admin.payment.prod.example.com`,
    description: "支付网关生产环境 Hosts 配置",
    environment: "生产",
    createdAt: "2023-05-31T11:10:00Z",
    updatedAt: "2023-05-31T11:10:00Z",
    createdBy: "管理员",
  },
  {
    id: "6",
    name: "商品管理开发环境",
    projectId: "4",
    moduleId: "6",
    content: `# 商品管理开发环境
172.172.178.14 product.dev.example.com
172.172.178.14 api.product.dev.example.com
# 172.172.178.14 admin.product.dev.example.com`,
    description: "商品管理开发环境 Hosts 配置",
    environment: "开发",
    createdAt: "2023-06-01T16:30:00Z",
    updatedAt: "2023-06-01T16:30:00Z",
    createdBy: "管理员",
  },
  {
    id: "7",
    name: "用户中心项目通用配置",
    projectId: "1",
    content: `# 用户中心项目通用配置
172.172.178.15 common.user.dev.example.com
172.172.178.15 api.common.user.dev.example.com`,
    description: "用户中心项目级通用 Hosts 配置",
    environment: "开发",
    createdAt: "2023-06-02T08:30:00Z",
    updatedAt: "2023-06-02T08:30:00Z",
    createdBy: "管理员",
  },
  {
    id: "8",
    name: "订单系统项目通用配置",
    projectId: "2",
    content: `# 订单系统项目通用配置
172.172.178.16 common.order.test.example.com
172.172.178.16 api.common.order.test.example.com`,
    description: "订单系统项目级通用 Hosts 配置",
    environment: "测试",
    createdAt: "2023-06-03T10:15:00Z",
    updatedAt: "2023-06-03T10:15:00Z",
    createdBy: "管理员",
  },
];

// Mock data for logs
const mockLogs: Log[] = [
  {
    id: "1",
    action: "create",
    targetType: "project",
    targetId: "1",
    targetName: "用户中心",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-15T08:30:00Z",
    details: '创建了项目 "用户中心"',
  },
  {
    id: "2",
    action: "create",
    targetType: "module",
    targetId: "1",
    targetName: "用户认证模块",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-21T13:45:00Z",
    details: '创建了模块 "用户认证模块"',
  },
  {
    id: "3",
    action: "create",
    targetType: "hostsBlock",
    targetId: "1",
    targetName: "用户中心开发环境",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-27T08:30:00Z",
    details: '创建了 Hosts 配置 "用户中心开发环境"',
  },
  {
    id: "4",
    action: "update",
    targetType: "hostsBlock",
    targetId: "1",
    targetName: "用户中心开发环境",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-27T09:30:00Z",
    details: '更新了 Hosts 配置 "用户中心开发环境"',
  },
  {
    id: "5",
    action: "create",
    targetType: "project",
    targetId: "2",
    targetName: "订单系统",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-16T10:15:00Z",
    details: '创建了项目 "订单系统"',
  },
  {
    id: "6",
    action: "delete",
    targetType: "hostsBlock",
    targetId: "9",
    targetName: "测试配置",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-06-04T11:20:00Z",
    details: '删除了 Hosts 配置 "测试配置"',
  },
  {
    id: "7",
    action: "update",
    targetType: "project",
    targetId: "3",
    targetName: "支付网关",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-18T10:30:00Z",
    details: '更新了项目 "支付网关"',
  },
  {
    id: "8",
    action: "create",
    targetType: "module",
    targetId: "5",
    targetName: "支付接口模块",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-25T11:40:00Z",
    details: '创建了模块 "支付接口模块"',
  },
  {
    id: "9",
    action: "create",
    targetType: "hostsBlock",
    targetId: "5",
    targetName: "支付网关生产环境",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-05-31T11:10:00Z",
    details: '创建了 Hosts 配置 "支付网关生产环境"',
  },
  {
    id: "10",
    action: "delete",
    targetType: "module",
    targetId: "7",
    targetName: "测试模块",
    userId: "1",
    userName: "管理员",
    timestamp: "2023-06-05T14:25:00Z",
    details: '删除了模块 "测试模块"',
  },
];

// Mock data for hosts
const mockHosts: Host[] = [];

// API URL configuration
const API_BASE_URL = "http://localhost:3001/api";

// Helper function for API requests
const apiRequest = async <T>(
  endpoint: string,
  options?: RequestInit
): Promise<T> => {
  const response = await fetch(`${API_BASE_URL}${endpoint}`, {
    headers: {
      "Content-Type": "application/json",
    },
    ...options,
  });

  if (!response.ok) {
    throw new Error(`API request failed: ${response.statusText}`);
  }

  // For DELETE requests with 204 No Content, return empty object
  if (response.status === 204) {
    return {} as T;
  }

  return (await response.json()) as T;
};

// API functions
export const fetchHosts = (): Promise<Host[]> => {
  return apiRequest<Host[]>("/hosts");
};

export const fetchDepartments = (): Promise<Department[]> => {
  return apiRequest<Department[]>("/departments");
};

export const fetchProjects = (): Promise<Project[]> => {
  return apiRequest<Project[]>("/projects");
};

export const fetchModules = (): Promise<Module[]> => {
  return apiRequest<Module[]>("/modules");
};

export const fetchHostsBlocks = (): Promise<HostsBlock[]> => {
  return apiRequest<HostsBlock[]>("/hostsBlocks");
};

export const fetchLogs = (): Promise<Log[]> => {
  return apiRequest<Log[]>("/logs");
};

export const fetchHostTypes = (): Promise<HostTypeDefinition[]> => {
  return apiRequest<HostTypeDefinition[]>("/hostTypes");
};

// Create, Update, Delete functions for hosts
export const createHost = (
  host: Omit<Host, "id" | "lastUpdated">
): Promise<Host> => {
  return apiRequest<Host>("/hosts", {
    method: "POST",
    body: JSON.stringify(host),
  });
};

export const updateHost = (id: string, host: Partial<Host>): Promise<Host> => {
  return apiRequest<Host>(`/hosts/${id}`, {
    method: "PUT",
    body: JSON.stringify(host),
  });
};

export const deleteHost = (id: string): Promise<void> => {
  return apiRequest<void>(`/hosts/${id}`, {
    method: "DELETE",
  });
};

// Create, Update, Delete functions for departments
export const createDepartment = (
  department: Omit<Department, "id">
): Promise<Department> => {
  return apiRequest<Department>("/departments", {
    method: "POST",
    body: JSON.stringify(department),
  });
};

export const updateDepartment = (
  id: string,
  department: Partial<Department>
): Promise<Department> => {
  return apiRequest<Department>(`/departments/${id}`, {
    method: "PUT",
    body: JSON.stringify(department),
  });
};

export const deleteDepartment = (id: string): Promise<void> => {
  return apiRequest<void>(`/departments/${id}`, {
    method: "DELETE",
  });
};

// Create, Update, Delete functions for projects
export const createProject = (
  project: Omit<Project, "id" | "createdAt">
): Promise<Project> => {
  return apiRequest<Project>("/projects", {
    method: "POST",
    body: JSON.stringify(project),
  });
};

export const updateProject = (
  id: string,
  project: Partial<Project>
): Promise<Project> => {
  return apiRequest<Project>(`/projects/${id}`, {
    method: "PUT",
    body: JSON.stringify(project),
  });
};

export const deleteProject = (id: string): Promise<void> => {
  return apiRequest<void>(`/projects/${id}`, {
    method: "DELETE",
  });
};

// Create, Update, Delete functions for modules
export const createModule = (
  module: Omit<Module, "id" | "createdAt">
): Promise<Module> => {
  return apiRequest<Module>("/modules", {
    method: "POST",
    body: JSON.stringify(module),
  });
};

export const updateModule = (
  id: string,
  module: Partial<Module>
): Promise<Module> => {
  return apiRequest<Module>(`/modules/${id}`, {
    method: "PUT",
    body: JSON.stringify(module),
  });
};

export const deleteModule = (id: string): Promise<void> => {
  return apiRequest<void>(`/modules/${id}`, {
    method: "DELETE",
  });
};

// Create, Update, Delete functions for hostsBlocks
export const createHostsBlock = (
  hostsBlock: Omit<HostsBlock, "id" | "createdAt" | "updatedAt" | "createdBy">
): Promise<HostsBlock> => {
  return apiRequest<HostsBlock>("/hostsBlocks", {
    method: "POST",
    body: JSON.stringify(hostsBlock),
  });
};

export const updateHostsBlock = (
  id: string,
  hostsBlock: Partial<HostsBlock>
): Promise<HostsBlock> => {
  return apiRequest<HostsBlock>(`/hostsBlocks/${id}`, {
    method: "PUT",
    body: JSON.stringify(hostsBlock),
  });
};

export const deleteHostsBlock = (id: string): Promise<void> => {
  return apiRequest<void>(`/hostsBlocks/${id}`, {
    method: "DELETE",
  });
};
